﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using System.IO;
using Charities.ModelViews;

namespace Charities
{
    public partial class Inspectors_Worthies_Report : Form
    {
        int userid;
        public bool is_loaded = false;
        ModelContainer ctx = new ModelContainer();
        Form fo = null;
        public Inspectors_Worthies_Report(int userId,Form fm)
        {
            fo = fm;

            MdiParent = fm;
            userid = userId;
            InitializeComponent();
        }

        private void Inspectors_Worthies_Report_Load(object sender, EventArgs e)
        {
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            is_loaded = true;
        }

        private void Inspectors_Worthies_Report_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            var allNeeds = ctx.Needs.ToList();
            if (dtp_FromDate.Focused)
            {
                var fromNeeds = allNeeds.
                    Where(c => c.InsertDate.Value.Year >= dtp_FromDate.Value.Year
                    && c.InsertDate.Value.Month >= dtp_FromDate.Value.Month
                    && c.InsertDate.Value.Day >= dtp_FromDate.Value.Day).ToList();
                allNeeds = fromNeeds;

            }
            if (dtp_ToDate.Focused)
            {
                var toNeeds = allNeeds.
                   Where(c => c.InsertDate.Value.Year <= dtp_FromDate.Value.Year
                && c.InsertDate.Value.Month <= dtp_FromDate.Value.Month
                && c.InsertDate.Value.Day <= dtp_FromDate.Value.Day).ToList();
                allNeeds = toNeeds;
            }
            List<ModelViews.InspectorsWorthiesView> wView = new List<ModelViews.InspectorsWorthiesView>();
            foreach (var n in allNeeds)
            {
                
                Person p = ctx.People.Single(c => c.Id == n.Worthy.PersonId);
                ModelViews.InspectorsWorthiesView wv = new ModelViews.InspectorsWorthiesView();
                wv.Id = n.Worthy.InspectorId;
                wv.insertDate = (DateTime)n.InsertDate;
                wv.InspectorDisplayName = n.Worthy.Inspector.Name;
                wv.Need = n.SupplyType.Name;
                wv.NeedDetail = n.Description;
                wv.WorthyDisplayName = p.FirstName + " " + p.FatherName + " " + p.LastName;
                if (dtp_FromDate.Focused)
                    wv.FromDate = dtp_FromDate.Value;
                else
                    wv.FromDate = (DateTime)allNeeds.First().InsertDate;
                if (dtp_ToDate.Focused)
                    wv.ToDate = dtp_ToDate.Value;
                else
                    wv.ToDate = (DateTime)allNeeds.Last().InsertDate;
                wView.Add(wv);
            }
            InspectorsWorthiesViewBindingSource.DataSource = wView;
            if (ctx.CharityInfos.Count() > 0)
            {
                CharityInfoBindingSource.DataSource = ctx.CharityInfos.First();
            }
            else
            {
                MessageBox.Show("يجب تنصيب الاعدادات الرئيسية حتى تعمل التقارير بالشكل الامثل ", "تنويه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.reportViewer1.RefreshReport();

        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

        private void Inspectors_Worthies_Report_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}
